////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2008 Michiel Helvensteijn                                    //
//                                                                            //
// This file is part of the Mist compiler.                                    //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modif   //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// along with the Mist compiler.  If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////

#include "messages.hpp"

/******************************************************************************
 * Includes                                                                   *
 ******************************************************************************/

#include <boost/foreach.hpp>
#define foreach BOOST_FOREACH

#include "assert.hpp"

/******************************************************************************
 * Function Implementations                                                   *
 ******************************************************************************/

Message& Messages::add(const Message& message) {
	vector<Message>::iterator it;
	unsigned count = 0;
	for (it = _messages.begin(); it != _messages.end(); ++it) {
		if (message < *it) break;
		++count;
	}
	
	_messages.insert(it, message);
	
	return _messages[count];
}

Message & Messages::add(MessageSeverity severity, const string& message) {
	return add(Message(severity, message));
}

Message & Messages::add(MessageSeverity severity, CodeLocation location, const string& message) {
	return add(Message(severity, location, message));
}

Message& Messages::add(const string& code) {
	return add(Message(code));
}

Message& Messages::add(const string& code, CodeLocation location) {
	return add(Message(code, location));
}

unsigned int Messages::count() const {
	return _messages.size();
}

unsigned int Messages::count(MessageSeverity severity) const {
	unsigned int result = 0;
	foreach (Message message, _messages)
		result += (message.severity() == severity);
	
	return result;
}

unsigned int Messages::count(const string& code) const {
	unsigned int result = 0;
	foreach (Message message, _messages)
		result += (message.code() == code);
	
	return result;
}

const Message& Messages::get(unsigned int index) const {
	assert(index < count());
	
	return _messages[index];
}

ostream& Messages::report_to_stream(ostream& out) const {
	foreach (Message message, _messages)
		out << message;
	
	return out;
}

ostream& operator<<(ostream& out, const Messages& messages) {
	return messages.report_to_stream(out);
}

Messages::iterator Messages::begin() {
	return _messages.begin();
}

Messages::iterator Messages::end() {
	return _messages.end();
}

Messages::const_iterator Messages::begin() const {
	return _messages.begin();
}

Messages::const_iterator Messages::end() const {
	return _messages.end();
}
